package cn.com.thingo.shyintelligent.ui.activity;


import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.th.opensdk.openapi.RemoterInfo;
import com.th.opensdk.openapi.RemoterKeyInfo;
import com.th.opensdk.openapi.THOpenSDK;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import butterknife.BindView;
import butterknife.OnClick;
import cn.com.thingo.shyintelligent.R;
import cn.com.thingo.shyintelligent.base.BaseActivity;
import cn.com.thingo.shyintelligent.base.contract.AirConditionControlContract;
import cn.com.thingo.shyintelligent.presenter.AirConditionControlPresenter;

public class AirConditionControlActivity extends BaseActivity<AirConditionControlPresenter> implements AirConditionControlContract.View {
    private THOpenSDK thOpenSDK;
    private List<RemoterKeyInfo> remoterKeyList = new ArrayList<>();
    @BindView(R.id.toolbar_left)
    ImageView ivBack;
    @BindView(R.id.toolbar_title)
    TextView tvTitle;
    private long remoter_id;

    @BindView(R.id.air_condition_open)
    ImageView ivOpen;
    @BindView(R.id.air_condition_close)
    ImageView ivClose;
    @BindView(R.id.air_condition_mode_cold)
    TextView tvCold;
    @BindView(R.id.air_condition_cold_18)
    TextView tvCold18;
    @BindView(R.id.air_condition_cold_20)
    TextView tvCold20;
    @BindView(R.id.air_condition_cold_24)
    TextView tvCold24;
    @BindView(R.id.air_condition_cold_26)
    TextView tvCold26;
    @BindView(R.id.air_condition_mode_hot)
    TextView tvHot;
    @BindView(R.id.air_condition_hot_20)
    TextView tvHot20;
    @BindView(R.id.air_condition_hot_22)
    TextView tvHot22;
    @BindView(R.id.air_condition_hot_24)
    TextView tvHot24;
    @BindView(R.id.air_condition_hot_26)
    TextView tvHot26;
    @BindView(R.id.air_condition_mode_dehumidification)
    TextView tvDehumidification;
    @BindView(R.id.air_condition_mode_auto)
    TextView tvAuto;
    @BindView(R.id.air_condition_mode_name)
    TextView tvModeName;
    @BindView(R.id.air_condition_mode_temperature)
    TextView tvTemperature;

    private List<TextView> modeList = new ArrayList<>();
    private List<TextView> temperatureList = new ArrayList<>();
    private RemoterKeyInfo closeKey;
    private RemoterKeyInfo openKey;
    private RemoterKeyInfo coldKey;
    private RemoterKeyInfo hotKey;
    private RemoterKeyInfo cold18Key;
    private RemoterKeyInfo cold20Key;
    private RemoterKeyInfo cold24Key;
    private RemoterKeyInfo cold26Key;
    private RemoterKeyInfo hot20Key;
    private RemoterKeyInfo hot22Key;
    private RemoterKeyInfo hot24Key;
    private RemoterKeyInfo hot26Key;
    private RemoterKeyInfo dehumidificationKey;
    private RemoterKeyInfo autoKey;
    private RemoterInfo remoterInfo;


    @Override
    protected int getLayout() {
        return R.layout.activity_air_condition_control;
    }

    @Override
    protected void initEventAndData() {
        thOpenSDK = THOpenSDK.getInstance();
        initView();
        initData();

    }

    private void initData() {
        remoterInfo = getRemoterInfo(remoter_id);
        Map<Integer, RemoterKeyInfo> keyList = remoterInfo.getKeyList();
        if (keyList != null && keyList.size() > 0) {
            remoterKeyList.clear();
            Set<Integer> integers = keyList.keySet();
            for (Integer next : integers) {
                RemoterKeyInfo remoterKeyInfo = keyList.get(next);
                remoterKeyList.add(remoterKeyInfo);
            }
        }
        for (int i = 0; i < remoterKeyList.size(); i++) {
            RemoterKeyInfo remoterKeyInfo = remoterKeyList.get(i);
            switch (remoterKeyInfo.getKeyName()) {
                case "关闭":
                    closeKey = remoterKeyInfo;
                    break;
                case "打开":
                    openKey = remoterKeyInfo;
                    break;
                case "制冷":
                    coldKey = remoterKeyInfo;
                    break;
                case "制热":
                    hotKey = remoterKeyInfo;
                    break;
                case "制冷-18":
                    cold18Key = remoterKeyInfo;
                    break;
                case "制冷-20":
                    cold20Key = remoterKeyInfo;
                    break;
                case "制冷-24":
                    cold24Key = remoterKeyInfo;
                    break;
                case "制冷-26":
                    cold26Key = remoterKeyInfo;
                    break;
                case "制热-20":
                    hot20Key = remoterKeyInfo;
                    break;
                case "制热-22":
                    hot22Key = remoterKeyInfo;
                    break;
                case "制热-24":
                    hot24Key = remoterKeyInfo;
                    break;
                case "制热-26":
                    hot26Key = remoterKeyInfo;
                    break;
                case "除湿":
                    dehumidificationKey = remoterKeyInfo;
                    break;
                case "自动":
                    autoKey = remoterKeyInfo;
                    break;
            }
        }
    }

    private void initView() {
        ivBack.setImageResource(R.mipmap.back);
        remoter_id = getIntent().getLongExtra("remoter_id", -1);
        tvTitle.setText(getRemoterInfo(remoter_id).getRemoterName());
        modeList.add(tvCold);
        modeList.add(tvHot);
        modeList.add(tvAuto);
        modeList.add(tvDehumidification);
        temperatureList.add(tvCold18);
        temperatureList.add(tvCold20);
        temperatureList.add(tvCold24);
        temperatureList.add(tvCold26);
        temperatureList.add(tvHot20);
        temperatureList.add(tvHot22);
        temperatureList.add(tvHot24);
        temperatureList.add(tvHot26);
    }

    private RemoterInfo getRemoterInfo(long remoter_id) {
        RemoterInfo result = null;
        List<RemoterInfo> remoterList = thOpenSDK.getRemoterList();
        for (int i = 0; i < remoterList.size(); i++) {
            if (remoter_id == remoterList.get(i).getRemoterId()) {
                result = remoterList.get(i);
            }
        }
        return result;
    }


    @OnClick({R.id.toolbar_left, R.id.air_condition_open, R.id.air_condition_close, R.id.air_condition_mode_cold, R.id.air_condition_cold_18,
            R.id.air_condition_cold_20, R.id.air_condition_cold_24, R.id.air_condition_cold_26, R.id.air_condition_mode_hot,
            R.id.air_condition_hot_20, R.id.air_condition_hot_22, R.id.air_condition_hot_24, R.id.air_condition_hot_26, R.id.air_condition_mode_dehumidification,
            R.id.air_condition_mode_auto})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.toolbar_left:
                finish();
                break;
            case R.id.air_condition_open:
                //开
                if (openKey != null) {
                    ivOpen.setVisibility(View.GONE);
                    ivClose.setVisibility(View.VISIBLE);
                    remoterInfo.sendRemoterCode(openKey.getKeyId(), RemoterKeyInfo.REMOTER_ACTION_DIRECT, 0);
                    break;
                }
            case R.id.air_condition_close:
                //关
                if (closeKey != null) {
                    ivOpen.setVisibility(View.VISIBLE);
                    ivClose.setVisibility(View.GONE);
                    tvTemperature.setVisibility(View.GONE);
                    tvModeName.setText("自动");
                    cleanMode();
                    cleanTemperature();
                    remoterInfo.sendRemoterCode(closeKey.getKeyId(), RemoterKeyInfo.REMOTER_ACTION_DIRECT, 0);
                }
                break;
            case R.id.air_condition_mode_cold:
                //制冷
                if (coldKey != null) {
                    tvModeName.setText("制冷");
                    cleanMode();
                    tvCold.setTextColor(getResources().getColor(R.color.app_color));
                    tvCold.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.mipmap.air_condition_cold), null, null);
                    remoterInfo.sendRemoterCode(coldKey.getKeyId(), RemoterKeyInfo.REMOTER_ACTION_DIRECT, 0);
                }
                break;
            case R.id.air_condition_cold_18:
                //制冷18度
                if (cold18Key != null) {
                    tvTemperature.setText("18℃");
                    tvModeName.setText("制冷");
                    tvTemperature.setVisibility(View.VISIBLE);
                    cleanTemperature();
                    tvCold18.setTextColor(getResources().getColor(R.color.app_color));
                    tvCold18.setBackground(getResources().getDrawable(R.drawable.temperature_bg_select));
                    cleanMode();
                    tvCold.setTextColor(getResources().getColor(R.color.app_color));
                    tvCold.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.mipmap.air_condition_cold), null, null);
                    remoterInfo.sendRemoterCode(cold18Key.getKeyId(), RemoterKeyInfo.REMOTER_ACTION_DIRECT, 0);
                }
                break;
            case R.id.air_condition_cold_20:
                //制冷20度
                if (cold20Key != null) {
                    tvTemperature.setText("20℃");
                    tvModeName.setText("制冷");
                    tvTemperature.setVisibility(View.VISIBLE);
                    cleanTemperature();
                    tvCold20.setTextColor(getResources().getColor(R.color.app_color));
                    tvCold20.setBackground(getResources().getDrawable(R.drawable.temperature_bg_select));
                    cleanMode();
                    tvCold.setTextColor(getResources().getColor(R.color.app_color));
                    tvCold.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.mipmap.air_condition_cold), null, null);
                    remoterInfo.sendRemoterCode(cold20Key.getKeyId(), RemoterKeyInfo.REMOTER_ACTION_DIRECT, 0);
                }
                break;
            case R.id.air_condition_cold_24:
                //制冷24度
                if (cold24Key != null) {
                    tvTemperature.setText("24℃");
                    tvModeName.setText("制冷");
                    tvTemperature.setVisibility(View.VISIBLE);
                    cleanTemperature();
                    tvCold24.setTextColor(getResources().getColor(R.color.app_color));
                    tvCold24.setBackground(getResources().getDrawable(R.drawable.temperature_bg_select));
                    cleanMode();
                    tvCold.setTextColor(getResources().getColor(R.color.app_color));
                    tvCold.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.mipmap.air_condition_cold), null, null);
                    remoterInfo.sendRemoterCode(cold24Key.getKeyId(), RemoterKeyInfo.REMOTER_ACTION_DIRECT, 0);
                }
                break;
            case R.id.air_condition_cold_26:
                //制冷26度
                if (cold26Key != null) {
                    tvTemperature.setText("26℃");
                    tvModeName.setText("制冷");
                    tvTemperature.setVisibility(View.VISIBLE);
                    cleanTemperature();
                    tvCold26.setTextColor(getResources().getColor(R.color.app_color));
                    tvCold26.setBackground(getResources().getDrawable(R.drawable.temperature_bg_select));
                    cleanMode();
                    tvCold.setTextColor(getResources().getColor(R.color.app_color));
                    tvCold.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.mipmap.air_condition_cold), null, null);
                    remoterInfo.sendRemoterCode(cold26Key.getKeyId(), RemoterKeyInfo.REMOTER_ACTION_DIRECT, 0);
                }
                break;
            case R.id.air_condition_mode_hot:
                //制热
                if (hotKey != null) {
                    tvModeName.setText("制热");
                    cleanMode();
                    tvHot.setTextColor(getResources().getColor(R.color.app_color));
                    tvHot.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.mipmap.air_condition_hot), null, null);
                    remoterInfo.sendRemoterCode(hotKey.getKeyId(), RemoterKeyInfo.REMOTER_ACTION_DIRECT, 0);
                }
                break;
            case R.id.air_condition_hot_20:
                //制热20度
                if (hot20Key != null) {
                    tvModeName.setText("制热");
                    tvTemperature.setText("20℃");
                    tvTemperature.setVisibility(View.VISIBLE);
                    cleanTemperature();
                    tvHot20.setTextColor(getResources().getColor(R.color.app_color));
                    tvHot20.setBackground(getResources().getDrawable(R.drawable.temperature_bg_select));
                    cleanMode();
                    tvHot.setTextColor(getResources().getColor(R.color.app_color));
                    tvHot.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.mipmap.air_condition_hot), null, null);
                    remoterInfo.sendRemoterCode(hot20Key.getKeyId(), RemoterKeyInfo.REMOTER_ACTION_DIRECT, 0);
                }
                break;
            case R.id.air_condition_hot_22:
                //制热22度
                if (hot22Key != null) {
                    tvModeName.setText("制热");
                    tvTemperature.setText("22℃");
                    tvTemperature.setVisibility(View.VISIBLE);
                    cleanTemperature();
                    tvHot22.setTextColor(getResources().getColor(R.color.app_color));
                    tvHot22.setBackground(getResources().getDrawable(R.drawable.temperature_bg_select));
                    cleanMode();
                    tvHot.setTextColor(getResources().getColor(R.color.app_color));
                    tvHot.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.mipmap.air_condition_hot), null, null);
                    remoterInfo.sendRemoterCode(hot22Key.getKeyId(), RemoterKeyInfo.REMOTER_ACTION_DIRECT, 0);
                }
                break;
            case R.id.air_condition_hot_24:
                //制热24度
                if (hot24Key != null) {
                    tvModeName.setText("制热");
                    tvTemperature.setText("24℃");
                    tvTemperature.setVisibility(View.VISIBLE);
                    cleanTemperature();
                    tvHot24.setTextColor(getResources().getColor(R.color.app_color));
                    tvHot24.setBackground(getResources().getDrawable(R.drawable.temperature_bg_select));
                    cleanMode();
                    tvHot.setTextColor(getResources().getColor(R.color.app_color));
                    tvHot.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.mipmap.air_condition_hot), null, null);
                    remoterInfo.sendRemoterCode(hot24Key.getKeyId(), RemoterKeyInfo.REMOTER_ACTION_DIRECT, 0);
                }
                break;
            case R.id.air_condition_hot_26:
                //制热26度
                if (hot26Key != null) {
                    tvModeName.setText("制热");
                    tvTemperature.setText("26℃");
                    tvTemperature.setVisibility(View.VISIBLE);
                    cleanTemperature();
                    tvHot26.setTextColor(getResources().getColor(R.color.app_color));
                    tvHot26.setBackground(getResources().getDrawable(R.drawable.temperature_bg_select));
                    cleanMode();
                    tvHot.setTextColor(getResources().getColor(R.color.app_color));
                    tvHot.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.mipmap.air_condition_hot), null, null);
                    remoterInfo.sendRemoterCode(hot26Key.getKeyId(), RemoterKeyInfo.REMOTER_ACTION_DIRECT, 0);
                }
                break;
            case R.id.air_condition_mode_dehumidification:
                //除湿
                if (dehumidificationKey != null) {
                    tvTemperature.setVisibility(View.GONE);
                    tvModeName.setText("除湿");
                    cleanMode();
                    cleanTemperature();
                    tvDehumidification.setTextColor(getResources().getColor(R.color.app_color));
                    tvDehumidification.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.mipmap.air_condition_dehumidification), null, null);
                    remoterInfo.sendRemoterCode(dehumidificationKey.getKeyId(), RemoterKeyInfo.REMOTER_ACTION_DIRECT, 0);
                }
                break;
            case R.id.air_condition_mode_auto:
                //自动
                if (autoKey != null) {
                    tvTemperature.setVisibility(View.GONE);
                    tvModeName.setText("自动");
                    cleanMode();
                    cleanTemperature();
                    tvAuto.setTextColor(getResources().getColor(R.color.app_color));
                    tvAuto.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.mipmap.air_condition_auto), null, null);
                    remoterInfo.sendRemoterCode(autoKey.getKeyId(), RemoterKeyInfo.REMOTER_ACTION_DIRECT, 0);
                }
                break;
        }
    }

    private void cleanTemperature() {
        if (temperatureList.size() > 0) {
            for (int i = 0; i < temperatureList.size(); i++) {
                TextView textView = temperatureList.get(i);
                textView.setBackground(getResources().getDrawable(R.drawable.temperature_bg_unselect));
                textView.setTextColor(getResources().getColor(R.color.gray_3));
            }
        }
    }

    private void cleanMode() {
        if (modeList.size() > 0) {
            for (int i = 0; i < modeList.size(); i++) {
                TextView textView = modeList.get(i);
                textView.setTextColor(getResources().getColor(R.color.gray_3));
                switch (textView.getText().toString().trim()) {
                    case "制冷":
                        textView.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.mipmap.air_condition_cold_un), null, null);
                        break;
                    case "制热":
                        textView.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.mipmap.air_condition_hot_un), null, null);
                        break;
                    case "除湿":
                        textView.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.mipmap.air_condition_dehumidification_un), null, null);
                        break;
                    case "自动":
                        textView.setCompoundDrawablesWithIntrinsicBounds(null, getResources().getDrawable(R.mipmap.air_condition_auto_un), null, null);
                        break;
                }

            }
        }
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

}
